package cn.elwy.rcp.ui.explorer.handler;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import com.yss.sofa.common.exception.RunException;
import com.yss.sofa.common.util.Assert;
import com.yss.sofa.ide.builder.dbscript.ui.dialog.MergeSqlDialog;
import com.yss.sofa.ide.builder.packer.AppContext;
import com.yss.sofa.ide.builder.packer.ProjectConst;
import com.yss.sofa.ide.builder.packer.service.ProjectConfigService;
import com.yss.sofa.ide.builder.packer.ui.project.ProjectUtil;
import com.yss.sofa.ide.builder.packer.vo.ProjectConfig;
import com.yss.sofa.ide.model.ui.progress.RunnableAdapter;
import com.yss.sofa.ide.ui.dialog.MsgDialog;
import com.yss.sofa.ide.ui.dialog.ProgressDialog;

public class MergeSqlHandler implements IHandler {

  private ProjectConfig projectConfig;
  private IProject project;

  @Override
  public void addHandlerListener(IHandlerListener handlerListener) {
  }

  @Override
  public void dispose() {
  }

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = HandlerUtil.getActiveShell(event);
    try {
      project = ProjectUtil.getSelectionProject(event);
      Assert.notNull(project, "请选择工程");
      String taskName = "加载信息";
      Shell currentShell = Display.getCurrent().getActiveShell();
      int result = ProgressDialog.open(currentShell, getAdapterMethod(), taskName);
      if (result == 1) {
        MergeSqlDialog dialog = new MergeSqlDialog(shell, project, projectConfig);
        return dialog.open();
      }
      return null;
    } catch (Exception e) {
      MsgDialog.openWarn(shell, e.getMessage());
      return null;
    }
  }

  public RunnableAdapter getAdapterMethod() {
    RunnableAdapter runnableAdapter = new RunnableAdapter() {

      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        try {

          File file = project.getFile(ProjectConst.PACKER_FILE_NAME).getLocation().toFile();

          ProjectConfigService service = AppContext.getService(ProjectConfigService.ID, ProjectConfigService.class);
          projectConfig = service.load(file);
        } catch (RunException e) {
          throw e;
        } catch (Exception e) {
          throw new RunException("加载工程配置文件失败！", e);
        }
      }
    };
    return runnableAdapter;
  }

  @Override
  public boolean isEnabled() {
    return true;// AssertUtil.isNotEmpty(ProjectUtil.getSelectionProject(null));
  }

  @Override
  public boolean isHandled() {
    return true;
  }

  @Override
  public void removeHandlerListener(IHandlerListener handlerListener) {
  }

}
